package com.smart.campus.school.service.impl;

import com.smart.campus.common.annotation.SchoolDataScope;
import com.smart.campus.common.core.text.Convert;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.common.utils.StringUtils;
import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.common.CommonConstants;
import com.smart.campus.school.domain.ApproveList;
import com.smart.campus.school.domain.Msg;
import com.smart.campus.school.domain.TeacherApplyList;
import com.smart.campus.school.enums.MsgTypeEnum;
import com.smart.campus.school.mapper.ApproveListMapper;
import com.smart.campus.school.mapper.ClassInfoMapper;
import com.smart.campus.school.mapper.GradeMapper;
import com.smart.campus.school.mapper.TeacherApplyListMapper;
import com.smart.campus.school.service.IMsgService;
import com.smart.campus.school.service.ITeacherApplyListService;
import com.smart.campus.system.domain.SysPost;
import com.smart.campus.system.domain.SysRole;
import com.smart.campus.system.domain.SysUser;
import com.smart.campus.system.service.ISysDeptService;
import com.smart.campus.system.service.ISysPostService;
import com.smart.campus.system.service.ISysRoleService;
import com.smart.campus.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 老师请假申请Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-06-15
 */
@Service
public class TeacherApplyListServiceImpl implements ITeacherApplyListService
{
    @Autowired
    private TeacherApplyListMapper applyListMapper;
    @Autowired
    private ClassInfoMapper classInfoMapper;

    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private ApproveListMapper approveListMapper;
    @Autowired
    private IMsgService msgService;
    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysPostService postService;



    /**
     * 查询老师请假申请
     * 
     * @param id 老师请假申请ID
     * @return 老师请假申请
     */
    @Override
    public TeacherApplyList selectTeacherApplyListById(Long id)
    {

        TeacherApplyList applyList = applyListMapper.selectTeacherApplyListById(id);
        //获取审批人
        SysUser sysUser = this.userService.selectUserById( applyList.getTeacherId());

        String ancestors = sysUser.getDept().getAncestors();
        String[] deptIds = ancestors.split(",");
        for (int i = deptIds.length-1; i >= 0; i--) {
            //从后往前查询上级部门的审批人
            List<SysUser> userList = userService.getSpUserByParentDeptId(Long.valueOf(deptIds[i]));
            for (SysUser user : userList) {
                ApproveList approveList = new ApproveList();
                approveList.setApplyId(id);
                approveList.setType(2);
                applyList.setApproverIds(user.getUserId().toString());
                applyList.setApproverNames(user.getUserName());
                List<ApproveList> approveListList = approveListMapper.selectApproveListList(approveList);
                applyList.setApproveListList(approveListList);
                return applyList;
            }
        }

        return applyList;
    }

    /**
     * 查询老师请假申请列表
     * 
     * @param applyList 老师请假申请
     * @return 老师请假申请
     */
    @Override
    public List<TeacherApplyList> selectTeacherApplyListList(TeacherApplyList applyList)
    {
        return applyListMapper.selectTeacherApplyListList(applyList);
    }

    /**
     * 新增老师请假申请
     * 
     * @param applyList 老师请假申请
     * @return 结果
     */
    @Override
    public int insertTeacherApplyList(TeacherApplyList applyList)
    {
        applyList.setCreateTime(DateUtils.getNowDate());
        applyList.setStatus(1);
        applyList.setCreateBy(ShiroUtils.getSysUser().getUserName());
        //获取审批人
        String ancestors = ShiroUtils.getSysUser().getDept().getAncestors();
        String[] deptIds = ancestors.split(",");
        for (int i = deptIds.length-1; i >= 0; i--) {
            //从后往前查询上级部门的审批人
            List<SysUser> userList = userService.getSpUserByParentDeptId(Long.valueOf(deptIds[i]));
            for (SysUser user : userList) {
                applyList.setApproverIds(user.getUserId().toString());
                applyList.setApproverNames(user.getUserName());
                Msg msg = new Msg();
                msg.setUserType(CommonConstants.TEACHER);
                msg.setStatus(CommonConstants.NO);
                msg.setCreateTime(new Date());
                msg.setUserId(user.getUserId());
                msg.setUserName(user.getUserName());
                msg.setContent(MsgTypeEnum.T_APPLY.getName());
                msg.setMsgType(MsgTypeEnum.T_APPLY.getValue());
                msg.setMsgLink(MsgTypeEnum.T_APPLY.getLink());
                msgService.insertMsg(msg);
                return applyListMapper.insertTeacherApplyList(applyList);
            }
        }
        return applyListMapper.insertTeacherApplyList(applyList);
    }

    @Override
    public int insertWechatTeacherApplyList(TeacherApplyList applyList) {
        //获取审批人
        SysUser sysUser = this.userService.selectUserById(applyList.getTeacherId());
        applyList.setCreateTime(DateUtils.getNowDate());
        applyList.setStatus(1);
        applyList.setCreateBy(sysUser.getUserName());
        //获取审批人
        String ancestors = sysUser.getDept().getAncestors();
        String[] deptIds = ancestors.split(",");
        for (int i = deptIds.length-1; i >= 0; i--) {
            //从后往前查询上级部门的审批人
            List<SysUser> userList = userService.getSpUserByParentDeptId(Long.valueOf(deptIds[i]));
            for (SysUser user : userList) {
                applyList.setApproverIds(user.getUserId().toString());
                applyList.setApproverNames(user.getUserName());
                Msg msg = new Msg();
                msg.setUserType(CommonConstants.TEACHER);
                msg.setStatus(CommonConstants.NO);
                msg.setCreateTime(new Date());
                msg.setUserId(user.getUserId());
                msg.setUserName(user.getUserName());
                msg.setContent(MsgTypeEnum.T_APPLY.getName());
                msg.setMsgType(MsgTypeEnum.T_APPLY.getValue());
                msg.setMsgLink(MsgTypeEnum.T_APPLY.getLink());
                msgService.insertMsg(msg);
                return applyListMapper.insertTeacherApplyList(applyList);
            }
        }
        return applyListMapper.insertTeacherApplyList(applyList);
    }

    /**
     * 修改老师请假申请
     * 
     * @param applyList 老师请假申请
     * @return 结果
     */
    @Override
    public int updateTeacherApplyList(TeacherApplyList applyList)
    {
        applyList.setUpdateTime(DateUtils.getNowDate());
        applyList.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return applyListMapper.updateTeacherApplyList(applyList);
    }

    /**
     * 删除老师请假申请对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTeacherApplyListByIds(String ids)
    {
        return applyListMapper.deleteTeacherApplyListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除老师请假申请信息
     * 
     * @param id 老师请假申请ID
     * @return 结果
     */
    @Override
    public int deleteTeacherApplyListById(Long id)
    {
        return applyListMapper.deleteTeacherApplyListById(id);
    }

    @Override
    public int approveTeacherApplyList(TeacherApplyList applyList) {
        applyList.setUpdateTime(DateUtils.getNowDate());
        applyList.setUpdateBy(applyList.getApproverNames());
        return applyListMapper.updateTeacherApplyList(applyList);
    }


}
